﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tools.Core;
using Tools.Core.Entities;
using Tools.Services.Interfaces;

namespace Tools.Services
{
    public class MailSenderService : IMailSenderService
    {
        private readonly DB _db;
        public MailSenderService(DB db)
        {
            _db = db;
        }
        public async Task CreateSenderAsync(MailSender sender)
        {
            _db.MailSenders.Add(sender);
            await _db.SaveChangesAsync();
        }

        public async Task DeleteSenderAsync(int id)
        {
            var sender = await _db.MailSenders.FirstOrDefaultAsync(s => s.Id == id);
            if (sender != null)
            {
                _db.MailSenders.Remove(sender);
                await _db.SaveChangesAsync();
            }
        }

        public async Task<IEnumerable<MailSender>> GetAllSendersAsync()
        {
            return await _db.MailSenders.ToListAsync();
        }

        public async Task<MailSender> GetSenderAsync(int id)
        {
            return await _db.MailSenders.FirstOrDefaultAsync(s=>s.Id == id);
        }

        public async Task UpdateSenderAsync(MailSender sender)
        {
            _db.MailSenders.AddOrUpdate(sender);
            await _db.SaveChangesAsync();
        }
    }
}
